package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.TFrontPsyConsulatationTeacher;
import model.VFrontPsyConsultationTeacher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;
import util.toolkit.TimeUtil;

/**
 * 网站前端专家信息管理控制类
 * @author djf
 * @since 2018/12/12
 */
@Controller
@RequestMapping(value="/sysexpertmanage")
public class SysExpertManagementController {

	/**
	 * 添加一个专家信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	//TODO 添加一个专家信息
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "添加一个专家信息", 
			operationClassPath = "controller.SysTitleManagementController.addExpert(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站专家管理/专家信息添加")
	@RequestMapping(value="/addexpert")
	public String addExpert(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		TimeUtil time=new TimeUtil();
		TFrontPsyConsulatationTeacher expert=new TFrontPsyConsulatationTeacher();
		
		try {
			String realname=req.getParameter("realname");
			String sex=req.getParameter("sex");
			String phone=req.getParameter("phone");
			String describe=req.getParameter("describe");
			String photoid=req.getParameter("photoid");
			String QQ=req.getParameter("QQ");
			
			if(realname!=null && !realname.equals("") && sex!=null && !sex.equals("")
				&& phone!=null && !phone.equals("") && photoid!=null && !photoid.equals("")){
				expert.setRealname(realname);
				expert.setSex(sex);
				expert.setPhone(phone);
				expert.setDescribe(describe);
				expert.setPhotoid(Integer.parseInt(photoid));
				expert.setQq(QQ);
				expert.setCreatetime(time.toTimestamp(new Date()).toString());
				
				boolean row=DAOFactory.getFrontExpertDAO().addFrontExpert(expert);
				if(row){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "专家添加成功！");
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "专家添加失败！");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "专家添加失败！");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "专家添加失败！");
		}
		
		PrintWriter out=resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 删除一个专家信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	//TODO 删除一个专家信息
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "删除一个专家信息", 
			operationClassPath = "controller.SysTitleManagementController.deleteExpert(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站专家管理/专家信息查看")
	@RequestMapping(value="/deleteexpert")
	public String deleteExpert(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		
		try {
			String sysid=req.getParameter("sysid");
			
			if(sysid!=null && !sysid.equals("")){
				boolean row=DAOFactory.getFrontExpertDAO().deleteFrontExpert(Integer.parseInt(sysid));
				if(row){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "专家信息删除成功！");
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "专家信息删除失败！");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "专家信息删除失败！");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "专家信息删除失败！");
		}
		
		PrintWriter out=resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 修改一个专家信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	//TODO 修改一个专家信息
	@Log(operationType = Constant.OPERTATION_UPDATE, operationDescribe = "修改一个专家信息", 
			operationClassPath = "controller.SysTitleManagementController.updateExpert(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站专家管理/专家信息查看")
	@RequestMapping(value="/updateexpert")
	public String updateExpert(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		
		try {
			String sysid=req.getParameter("sysid");
			String realname=req.getParameter("realname");
			String sex=req.getParameter("sex");
			String phone=req.getParameter("phone");
			String describe=req.getParameter("describe");
			String QQ=req.getParameter("QQ");
			if(sysid!=null && !sysid.equals("") && realname!=null && !realname.equals("") 
					&& sex!=null && !sex.equals("") && phone!=null && !phone.equals("")){
				TFrontPsyConsulatationTeacher expert=DAOFactory.getFrontExpertDAO().getAFrontExpert(Integer.parseInt(sysid));
				expert.setRealname(realname);
				expert.setSex(sex);
				expert.setPhone(phone);
				expert.setDescribe(describe);
				expert.setQq(QQ);
				
				boolean row=DAOFactory.getFrontExpertDAO().updateFrontExpert(expert);
				if(row){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "专家信息修改成功！");
				}else{
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "专家信息修改失败！");
				}
			}else{
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "专家信息修改失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "专家信息修改失败！");
		}
		
		PrintWriter out=resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 根据查询条件得到专家信息列表 
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	//TODO 根据查询条件得到专家信息列表 
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "根据查询条件得到专家信息列表 ", 
			operationClassPath = "controller.SysTitleManagementController.getExpertList(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "网站专家管理/专家信息查看")
	@RequestMapping(value="/getexpertlist")
	public String getExpertList(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		Expression exp=new Expression();
		
		try {
			String currentPage=req.getParameter("page");
			String pageSize=req.getParameter("limit");
			String realname=req.getParameter("realname");
			
			if(realname!=null && !realname.equals("")){
				exp.andLike("realname", realname, String.class);
			}
			exp.orderByDesc("createtime");
			List<VFrontPsyConsultationTeacher> list=DAOFactory.getFrontExpertDAO().getFrontExpertList(
					exp.toString(), Integer.parseInt(currentPage), Integer.parseInt(pageSize));
			List<VFrontPsyConsultationTeacher> listsize=DAOFactory.getFrontExpertDAO().
					getFrontExpertList(exp.toString());
			if(list!=null && list.size()>0 && listsize!=null && listsize.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", list.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", 1);
				result.put("msg", "无查询结果");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 2);
			result.put("msg", "无查询结果");
		}
		
		
		PrintWriter out=resp.getWriter();
		out.println(result);
		return null;
	}
}
